package com.zq.ssmdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 张乔
 * @Date 2024/6/8 20:26
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.zq")
public class MVCConfig implements WebMvcConfigurer {
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver internalResourceViewResolver=new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }
    @Resource
    private MyHandlerInterface myHandlerInterface;
    @Override
    public   void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myHandlerInterface)
                .addPathPatterns("/**");
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**")
                .addResourceLocations("classpath:/static/image/");
    }


//    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
     StringHttpMessageConverter stringHttpMessageConverter=
             new StringHttpMessageConverter();
     List<MediaType> supportedMediaTypes=new ArrayList<>();
     supportedMediaTypes.add(MediaType.valueOf("application/json;charset=utf-8"));
     supportedMediaTypes.add(MediaType.valueOf("application/x-www-form-urlencoded"));
     supportedMediaTypes.add(MediaType.valueOf("text/html;charset=utf-8"));
        System.out.println("-------------------------------------");
     stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        return stringHttpMessageConverter;
    }

@Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver=new CommonsMultipartResolver();
     commonsMultipartResolver.setDefaultEncoding("UTF-8");
        commonsMultipartResolver.setMaxUploadSize(10*1024*1024);// 设置最大上传大小为10MB
        return commonsMultipartResolver;
    }


}
